package com.ihg.hiex.admin.handler.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.stereotype.Component;

import com.ihg.hiex.admin.handler.CalibrationHandler;
import com.ihg.hiex.entity.session.during.SessionAnchorScore;
import com.ihg.hiex.entity.session.during.SessionAnchorScoreType;

/**
 * 
 * @author dan
 *
 */
@Component("calibrationHandler")
public class CalibrationHandlerImpl implements CalibrationHandler{

    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.assessment.handler.CalibrationHandler#generateAnchorScore(java.util.Map, java.util.Map)
     */
    public Map<String, SessionAnchorScoreType> generateAnchorScore(Map<String, List<SessionAnchorScore>> meetingScores,
            Map<String, List<SessionAnchorScore>> inBasketScores) {
        Map<String, SessionAnchorScoreType> result = new HashMap<String, SessionAnchorScoreType>();
        
        Map<String, Map<String, Object>> scoreDatas = this.getScoreData(meetingScores, inBasketScores);
        for(Entry<String, Map<String, Object>> scoreData : scoreDatas.entrySet()) {
            if((boolean)scoreData.getValue().get("dnExist") && (boolean)scoreData.getValue().get("sExist")) {
                result.put(scoreData.getKey(), SessionAnchorScoreType.WARN);
            } else {
                int score = (Integer)scoreData.getValue().get("score")/(Integer)scoreData.getValue().get("amount");
                if(score >= 1 && score <= 2) {
                    result.put(scoreData.getKey(), SessionAnchorScoreType.DN);
                } else if(score == 3) {
                    result.put(scoreData.getKey(), SessionAnchorScoreType.C);
                } else if (score >= 4 && score <= 5){
                    result.put(scoreData.getKey(), SessionAnchorScoreType.S);
                } else {
                    result.put(scoreData.getKey(), null);
                }
            }
        }
        
        return result;
    }
    
    /**
     * 
     * @param meetingScores
     * @param inBasketScores
     * @return
     */
    private Map<String, Map<String, Object>> getScoreData(Map<String, List<SessionAnchorScore>> meetingScores,
            Map<String, List<SessionAnchorScore>> inBasketScores) {
        
        // 计算三个meeting所有的得分
        Map<String, Map<String, Object>> scoreDatas = new HashMap<String, Map<String, Object>>();
        for(Entry<String, List<SessionAnchorScore>> meetingScore : meetingScores.entrySet()) {
            Map<String, Object> scoreData = new HashMap<String, Object>();
            int score = 0;
            boolean dnExist = false;
            boolean sExist = false;
            for(SessionAnchorScore sessionAnchorScore : meetingScore.getValue()) {
                dnExist = sessionAnchorScore.getScore() != null ? sessionAnchorScore.getScore().isDn() : false;
                sExist = sessionAnchorScore.getScore() != null ? sessionAnchorScore.getScore().isS() : false;
                int currrentScore = sessionAnchorScore.getScore() != null ? sessionAnchorScore.getScore().getScore() : 0;
                score = score + currrentScore;
            }
            scoreData.put("amount", meetingScore.getValue().size());
            scoreData.put("score", score);
            scoreData.put("dnExist", dnExist);
            scoreData.put("sExist", sExist);
            scoreDatas.put(meetingScore.getKey(), scoreData);
        }
        
        // 计算 InBasket 得分
        for(Entry<String, List<SessionAnchorScore>> inBasketScore : inBasketScores.entrySet()) {
            Map<String, Object> scoreData = new HashMap<String, Object>();
            int amount = inBasketScore.getValue().size();
            int score = 0;
            boolean dnExist = false, sExist = false;
            for(SessionAnchorScore sessionAnchorScore : inBasketScore.getValue()) {
                dnExist = sessionAnchorScore.getScore() != null ? sessionAnchorScore.getScore().isDn() : false;
                sExist = sessionAnchorScore.getScore() != null ? sessionAnchorScore.getScore().isS() : false;
                int currrentScore = sessionAnchorScore.getScore() != null ? sessionAnchorScore.getScore().getScore() : 0;
                score = score + currrentScore;
            }
            if(scoreDatas.containsKey(inBasketScore.getKey())) {
                dnExist = dnExist ? dnExist : (boolean)scoreDatas.get(inBasketScore.getKey()).get("dnExist");
                sExist = sExist ? sExist : (boolean)scoreDatas.get(inBasketScore.getKey()).get("sExist");
                amount = amount + (Integer)scoreDatas.get(inBasketScore.getKey()).get("amount");
                score = score + (Integer)scoreDatas.get(inBasketScore.getKey()).get("score");
            }
            scoreData.put("amount", amount);
            scoreData.put("score", score);
            scoreData.put("dnExist", dnExist);
            scoreData.put("sExist", sExist);
            scoreDatas.put(inBasketScore.getKey(), scoreData);
        }
        return scoreDatas;
    }

}
